package com.wwwarehouse.taskcenter.fragment.job_point.step2.parent;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.GridView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CardDeskFunctionResponseBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.adapter.job_point.DimensionAdapter;
import com.wwwarehouse.taskcenter.bean.job_point.DimensionBean;
import com.wwwarehouse.taskcenter.bean.job_point.JobPointTypeResponseBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jackie on 2017/10/27
 * 父作业点维度列表
 */

public class ParentJobPointDimensionListFragment extends BaseFragment implements View.OnClickListener {
    private View mRootView;

    private StateLayout mStateLayout;
    private GridView mGridView;
    private DimensionAdapter mAdapter;
    private StateButton mSaveBtn;

    private CardDeskFunctionResponseBean.TaskBean mTaskDetailsBean;
    private List<JobPointTypeResponseBean> mJobPointTypeList;

    private static final int REQUEST_GET_JOB_POINT_TYPE = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_parent_job_point_dimension_list, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mStateLayout = findView(mRootView, R.id.sl_layout);
        mStateLayout.showProgressView(true);
        mGridView = findView(mRootView, R.id.gv_dimension);
        mSaveBtn = findView(mRootView, R.id.btn_save);
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            mTaskDetailsBean = (CardDeskFunctionResponseBean.TaskBean) bundle.getSerializable(Constant.KEY_CARD_DESK_FUNCTION_DETAILS);
        }
    }

    private void initEvent() {
        mSaveBtn.setOnClickListener(this);
    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {
        getJobPointType();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.bt_save) {

        }
    }

    NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {

        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_GET_JOB_POINT_TYPE:
                    if ("0".equals(commonClass.getCode())) {
                        mJobPointTypeList = JSON.parseArray(commonClass.getData().toString(), JobPointTypeResponseBean.class);

                        //构造本地数据
                        final List<DimensionBean> dimensionList = buildDimensionList(mJobPointTypeList);

                        mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                            @Override
                            public void onGlobalLayout() {
                                mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                                int itemHeight = (mGridView.getHeight() - mGridView.getVerticalSpacing() * 2) / 3;

                                mAdapter = new DimensionAdapter(mActivity, dimensionList, itemHeight);
                                mGridView.setAdapter(mAdapter);
                            }
                        });

                        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                DimensionBean dimensionBean = dimensionList.get(position);

                                // 跳转不同的维度
                                goDimension(dimensionBean);
                            }
                        });
                    } else {
                        mStateLayout.showSystemView(commonClass.getMsg(), true);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mStateLayout.showProgressView(true);

                                getJobPointType();
                            }
                        });
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            mStateLayout.showNetworkView(true);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mStateLayout.showProgressView(true);

                    getJobPointType();
                }
            });
        }

        @Override
        public void onFinish(int what) {
            mStateLayout.showContentView();
        }
    };

    private void getJobPointType() {
        Map map = new HashMap();

//        map.put("buId", mTaskDetailsBean.getBusinessId());
        //// TODO: 2017/10/28  
        map.put("buId", "10001356");
        map.put("type", "1");  //父作业点

        NoHttpUtils.httpPost(TaskCenterConstant.URL_GET_JOB_POINT_TYPE, map, mOnResponseListener, REQUEST_GET_JOB_POINT_TYPE);
    }

    private List<DimensionBean>  buildDimensionList(List<JobPointTypeResponseBean> jobPointTypeList) {
        List<DimensionBean> dimensionList = new ArrayList<>();

        for (JobPointTypeResponseBean jobPointTypeBean : jobPointTypeList) {
            if ("1".equals(jobPointTypeBean.getCombination())) {
                //组合
                DimensionBean dimensionBean = new DimensionBean();
                dimensionBean.setResId(R.drawable.dimension_combination);
                dimensionBean.setName("组合");
                dimensionBean.setFinish(false);

                dimensionList.add(dimensionBean);
            } else if ("1".equals(jobPointTypeBean.getPosition())) {
                //位置
                DimensionBean dimensionBean = new DimensionBean();
                dimensionBean.setResId(R.drawable.dimension_position);
                dimensionBean.setName("位置");
                dimensionBean.setFinish(false);

                dimensionList.add(dimensionBean);
            } else if ("1".equals(jobPointTypeBean.getAmount())) {
                //数量
                DimensionBean dimensionBean = new DimensionBean();
                dimensionBean.setResId(R.drawable.dimension_amount);
                dimensionBean.setName("数量");
                dimensionBean.setFinish(false);

                dimensionList.add(dimensionBean);
            } else if ("1".equals(jobPointTypeBean.getQuality())) {
                //品质
                DimensionBean dimensionBean = new DimensionBean();
                dimensionBean.setResId(R.drawable.dimension_quality);
                dimensionBean.setName("品质");
                dimensionBean.setFinish(false);

                dimensionList.add(dimensionBean);
            } else if ("1".equals(jobPointTypeBean.getUseright())) {
                //使用权
                DimensionBean dimensionBean = new DimensionBean();
                dimensionBean.setResId(R.drawable.dimension_useright);
                dimensionBean.setName("使用权");
                dimensionBean.setFinish(false);

                dimensionList.add(dimensionBean);
            } else if ("1".equals(jobPointTypeBean.getUseright())) {
                //所有权
                DimensionBean dimensionBean = new DimensionBean();
                dimensionBean.setResId(R.drawable.dimension_ownership);
                dimensionBean.setName("所有权");
                dimensionBean.setFinish(false);

                dimensionList.add(dimensionBean);
            } else if ("1".equals(jobPointTypeBean.getUseright())) {
                //处置权
                DimensionBean dimensionBean = new DimensionBean();
                dimensionBean.setResId(R.drawable.dimension_disposition);
                dimensionBean.setName("处置权");
                dimensionBean.setFinish(false);

                dimensionList.add(dimensionBean);
            } else if ("1".equals(jobPointTypeBean.getUseright())) {
                //委托权
                DimensionBean dimensionBean = new DimensionBean();
                dimensionBean.setResId(R.drawable.dimension_entrust);
                dimensionBean.setName("委托权");
                dimensionBean.setFinish(false);

                dimensionList.add(dimensionBean);
            } else if ("1".equals(jobPointTypeBean.getUseright())) {
                //物的转换
                DimensionBean dimensionBean = new DimensionBean();
                dimensionBean.setResId(R.drawable.dimension_item_change);
                dimensionBean.setName("物的转换");
                dimensionBean.setFinish(false);

                dimensionList.add(dimensionBean);
            }
        }

        return dimensionList;
    }

    private void goDimension(DimensionBean dimensionBean) {
        if ("组合".equals(dimensionBean.getName())) {
            pushFragment(new ChangeTypeParentCombinationFragment());
        } else if ("位置".equals(dimensionBean.getName())) {
            pushFragment(new ChangeTypeParentPositionFragment());
        } else if ("数量".equals(dimensionBean.getName())) {
            pushFragment(new ChangeTypeParentAmountFragment());
        } else if ("品质".equals(dimensionBean.getName())) {
            pushFragment(new ChangeTypeParentQualityFragment());
        } else if ("使用权".equals(dimensionBean.getName())) {
            pushFragment(new ChangeTypeParentUseRightFragment());
        } else if ("所有权".equals(dimensionBean.getName())) {
            pushFragment(new ChangeTypeParentOwnerShipFragment());
        } else if ("处置权".equals(dimensionBean.getName())) {
            pushFragment(new ChangeTypeParentDispositionFragment());
        } else if ("委托权".equals(dimensionBean.getName())) {
            pushFragment(new ChangeTypeParentEntrustFragment());
        } else if ("物的转换".equals(dimensionBean.getName())) {
            pushFragment(new ChangeTypeParentItemChangeFragment());
        }
    }
}
